package busuanzi.storage;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Auth: bruce-sha
 * Date: 2015/4/20
 * <p/>
 * http://www.programcreek.com/java-api-examples/index.php?api=redis.clients.jedis.JedisPool
 */
public class RedisPool {

    static private JedisPool pool;

    private static Jedis get() {
        if (pool == null) pool = new JedisPool("redis://:buru.redis@127.0.0.1:6379");
        return pool.getResource();
    }

    public static <T> T bath(RedisBather bather) {
        Jedis jedis = null;
        try {
            jedis = get();
            return (T) bather.bash(jedis);
        } finally {
            close(jedis);
        }
    }

    private static void close(Jedis jedis) {
        if (jedis != null) jedis.close();
    }

    public interface RedisBather {
        Object bash(Jedis jedis);
    }
}
